package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.databinding.MessageDetailsBinding;
import com.hisan.freeride.home.model.WMessage;
import com.lzy.okgo.OkGo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

import static com.hisan.freeride.R.layout.bind;


/**
 * 爱心消息详情
 * Created by Administrator on 2017/4/6.
 */

public class DLoveMessageActivtity extends BaseActivity<MessageDetailsBinding> {
    private Intent intent;
    private int id;

    @Override
    protected int getLayoutId() {
        return R.layout.message_details;
    }



    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        Bundle bundle=getIntent().getExtras();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
             id=bundle.getInt("id");
        }
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> {
            if (CollectionUtils.isNullOrEmpty(intent))intent=new Intent();
            setResult(99,intent);
            finishActivity(true);
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<WMessage>get(AppConfig.aDetail)
                .tag(this)
                .params("id",id)
                .execute(new DialogCallback<WMessage>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<WMessage> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            if (isOk(response)){
                                showData();
                                mBinding.mName.setText(response.body().getTitle());
                                mBinding.mData.setText(response.body().getCreate_time());
                                mBinding.titlename.setText(response.body().getTitle());
                                mBinding.trainingWebview.setText(response.body().getContent());
                            }else {
                                showErrorView(response.code());
                            }
                        }else {
                            noData();
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<WMessage> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }


}
